package com.leyou.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.dto.BrandDTO;
import com.leyou.mapper.BrandMapper;
import com.leyou.pojo.Brand;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //1.分页 limit 0,10
        PageHelper.startPage(page, rows);

        //2.准备搜索条件
        Example example = new Example(Brand.class);

        //2.1过滤条件 where `name` like "%x%" or letter = 'x'
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key);

        }
            //2.2排序条件 order by update_time
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);

            //3.查询数据，得到结果
            List<Brand> list = brandMapper.selectByExample(example);
            if (CollectionUtils.isEmpty(list)) {
                throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
            }

            //4.封装数据
            //4.1 分页结果
            PageInfo<Brand> info= new PageInfo<>(list);
            //4.2 dto转换
            List<BrandDTO> dtoList= BeanHelper.copyWithCollection(list,BrandDTO.class);
            return new PageResult<>(info.getTotal(),info.getPages(),dtoList);
        }
    @Transactional
    public void saveBrand(BrandDTO brandDTO,List<Long> ids){
        //新增品牌
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brandDTO.setId(null);
        int count = brandMapper.insertSelective(brand);
        if(count!=1){
            //新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //新增品牌和分类中间表
        count=brandMapper.insertCategoryBrand(brand.getId(),ids);
        if(count!=ids.size()){
            //新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    public BrandDTO queryById(Long id){
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if(brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }


    public List<BrandDTO> queryBrandByCategroyId(Long cid) {
        List<Brand> list=brandMapper.queryByCategroyId(cid);
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }

    public List<BrandDTO> queryByIds(List<Long> ids) {
        List<Brand> list = brandMapper.selectByIdList(ids);
        //判断是否为空
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }
}
